package com.kust.smarthome.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kust.smarthome.web.dto.EvaluateDto;
import com.kust.smarthome.web.model.Evaluate;
import com.kust.smarthome.web.model.User;
import com.kust.smarthome.web.service.IEvaluateService;
import com.kust.smarthome.web.service.IUserService;
import com.kust.smarthome.web.vo.EvaluateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/evaluate")
@Api(value = "评价相关操作", tags = {"shopping-tag"})
public class EvaluateController {

    @Resource
    private IEvaluateService evaluateService;

    @Resource
    private IUserService userService;

    @PostMapping("")
    @ApiOperation(value = "新增评价", notes = "用户填写表单增加一条评价")
    public boolean insertEvaluate(Principal principal, EvaluateVo evaluateVo) {

        User user = userService.getByUsername(principal.getName());

        return 1 == evaluateService.insertEvaluate(user.getId(), evaluateVo);
    }

    @GetMapping("/getEvaluateByCommodityId")
    @ApiOperation(value = "获取评价", notes = "通过CommodityId获取对应商品的评价")
    public List<EvaluateDto> getEvaluateByCommodityId(Integer commodityId) {

        List<EvaluateDto> list = evaluateService.getEvaluateByCommodityId(commodityId);

        return list;
    }

    @GetMapping("/deleteEvaluateById")
    @ApiOperation(value = "删除评价", notes = "用户删除对应评价")
    public boolean deleteEvaluateById(Principal principal, Integer id) {

        Evaluate evaluate = evaluateService.getById(id);
        User user = userService.getByUsername(principal.getName());
        if (evaluate.getUserId() == user.getId()) {
            return evaluateService.removeById(id);
        }

        return false;
    }

    @Value("file:\\\\10.1.188.49\\upload")
    private File resourcePath;

    @Value("http://xiangrikui.resource.vaiwan.com")
    private String resourceHost;

    @PostMapping("/upload/image")
    @ApiOperation("上传图片,返回图片路径")
    private String uploadFile(MultipartFile imageFile) throws IOException {

        String path = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDate.now());

        File folder = new File(resourcePath, path);
        folder.mkdirs();//在resourcePath路径+Path路径创建文件夹

        String fileName = imageFile.getOriginalFilename();//得到图片源文件名

        String ext = fileName.substring(fileName.lastIndexOf("."));//得到后缀名

        String name = UUID.randomUUID().toString() + ext;

        File file = new File(folder, name);
        imageFile.transferTo(file);

        String url = resourceHost + "/" + path + "/" + name;

        return url;

    }

}
